Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): support for historical expressions #185

Closed
wants to merge 20 commits into from

Conversation

arodidev
Copy link
Contributor

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR introduces support for historical expressions to the form's engine by leveraging the existing previous-value functionality, making the previous value available when either formField.historicalExpression or formField.questionOptions.enablePreviousValue resolve to true.

This is enabled by refactoring the questionOptions property usePreviousValueDisabled to enablePreviousValue to allow for lexical accuracy when using their resolved boolean values to enable and disable the previous-value functionality.

By default, the previous encounter will always be available in the form unless explicitly specified otherwise. ie formOptions.usePreviousValueDisabled == true and using this would make the historical expression functionality seize to work.

Screenshots

Related Issue

Other

@samuelmale
Copy link
Member

samuelmale commented Apr 16, 2024

@arodidev Are you planning on setting up the HD object in the expression runner's context to make it possible to resolve expressions like: "historicalExpression": "HD.getObject('prevEnc').getValue('a8a6619c-1350-11df-a1f1-0026b9348838')" out of the box?

Something like:

class HistoricalDataSourceService {
  dataSourceMap: Record<string, any> = {};

  putObject(key: string, value: any) {
    this.dataSourceMap[key] = value;
  }

  getObject(key: string) {
    return this.dataSourceMap[key];
  }
}

// Instantiate one
export const HD = new HistoricalDataSourceService();
// Setup previous encounter
HD.putObject('prevEnc', {
  value: encounterContex.previousEncounter,
  getValue(concept) {
    // lookup obs
    return this.value.obs.find((obs) => obs.concept.uuid == concept)?.value;
  },
});

// We then add the instance of `HistoricalDataSourceService ` to the context of the expression runner.

@arodidev
Copy link
Contributor Author

arodidev commented Apr 16, 2024

@arodidev Are you planning on setting up the HD object in the expression runner's context to make it possible to resolve expressions like: "historicalExpression": "HD.getObject('prevEnc').getValue('a8a6619c-1350-11df-a1f1-0026b9348838')" out of the box?

Not really, I believe we already support functionality for retrieving the field's previous value by concept UUID from the previous encounter, my implementation is just a reuse of the existing logic but with minor tweaks. Was the idea to implement the functionality you've mentioned above?

@samuelmale
Copy link
Member

@arodidev Well to the most part that may work, except for a few edge cases. If you checkout this Ampath form. It seems to paint a more complicated scenario:

{
  "label": "Line of ART patient is taking:",
  "id": "current_arv_line",
  "historicalExpression": "!_.isEmpty(HD.getObject('prevEnc').getValue('a89b6a62-1350-11df-a1f1-0026b9348838')) ? undefined : HD.getObject('prevEnc').getValue('04616f5d-b961-4f41-bbd7-bcc0dd235577')",
  "questionOptions": {
    "concept": "04616f5d-b961-4f41-bbd7-bcc0dd235577",
    "answers": [
      {
        "concept": "034047bd-3fa1-4b2a-b0f0-2787e9b9f7b3",
        "label": "First line regimen"
      },
      {
        "concept": "8f8a715d-e49a-4b2c-aa3a-83fa9d7a4254",
        "label": "Second line regimen"
      },
      {
        "concept": "a90ebdd2-351f-485a-b850-4938fcca2729",
        "label": "Third line regimen"
      }
    ],
    "rendering": "select"
  },
  "type": "obs",
  "validators": [
    {
      "type": "js_expression",
      "failsWhenExpression": "isEmpty(myValue) && !isEmpty(onArt) && onArt === 'a899b35c-1350-11df-a1f1-0026b9348838'",
      "message": "Patient previously marked as on ART. Please provide the treatment category."
    }
  ],
  "hide": {
    "hideWhenExpression": "onArt !== 'a899b35c-1350-11df-a1f1-0026b9348838'||  arrayContains (['6bf34991-7414-4b5e-ad75-ca217d4c4939'],visitTypeUuid)"
  }
}

Would the existing logic meet the semantics of the "historicalExpression" above?

@arodidev
Copy link
Contributor Author

@samuelmale I see, back to the drawing board it is...

@arodidev arodidev marked this pull request as ready for review April 24, 2024 16:12
pirupius and others added 11 commits April 24, 2024 20:20
* (BREAKING) Rename files and reorganize styles

rebase cleanups

reference refactors

* more refactors

* more clean ups

* forms folder changes and aligns styles

* rebase fixes

* more clean ups

* corrects rebase residue
* Implement i18next for @openmrs/esm-form-engine-lib

* Load translation resources in i18next on loading

* Loading translation resources when language is changed

* Translate form labels with fetched form translation resources

* Review changes

* Updated withFormEngineTranslations

* Fixed failing tests

* Cleanup
…expression (openmrs#196)

* feat: disbale multiCheckbox input options given provided expression

* chore: add unit tests

* bg: fix failed tests

* fix: fix value refrencing fields

* refactor: fix linting

* refactor: fix linting

* chore: add test case

* chore: rebase branch to main

* chore: fix tests

* refactor: fix lint
@arodidev arodidev closed this Apr 25, 2024
@arodidev arodidev changed the title (feat) O3-3029: support for historical expressions (feat): support for historical expressions Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants